<?php

namespace App\Admin\Extensions\ExcelExport;

use App\Models\Area;
use Encore\Admin\Grid\Exporters\AbstractExporter;
use Maatwebsite\Excel\Facades\Excel;

class RuralExpoter extends AbstractExporter
{
    public function export()
    {
        ini_set('memory_limit','500M');
        set_time_limit(0);//设置超时限制为0分钟
        Excel::create('常德市导游信息', function($excel) {
            $excel->sheet('导游', function($sheet) {
                // 这段逻辑是从表格数据中取出需要导出的字段
                $rows = collect($this->getData())->map(function ($item) {
                    $item['area_id'] = $item['area_id']?Area::find($item['area_id'])->title:'';
                    return array_only($item, ['id','area_id','name', 'title','address','tel']);
                });
                $sheet->prependRow(['ID', '区县','负责人/联系人', '名称','地址','电话']);
                $sheet->row(1, function($row) {
                    $row->setAlignment('center');
                    $row->setFont(array(
                        'size'       => '12',
                        'bold'       =>  true
                    ));
                });
                $sheet->rows($rows);
            });
        })->export('xls');
    }
}